#! /usr/bin/python
from chimera.core.cli import ChimeraCLI, action
import sys

class PhotometryCLI(ChimeraCLI):
    def __init__(self):
        ChimeraCLI.__init__(self, "chimera-db", "Photometry controller script", 0.1)
        self.addHelpGroup("Photometry", "Photometry")
        self.addController(name="phot", cls="Photometry", required=True, helpGroup="Photometry", help="Module to process photometry")
        self.addParameters(dict(name="stars", short="s", long="stars", type="string", default=None, helpGroup="Photometry", help="File containing a list of stars to filter by (optional)"))
        self.stars = None

    @action(short="f", type="string", helpGroup="Photometry", help="Run photometry on a single file")
    def file(self, options):
        if options.stars != None:
            self.getStars()

        if options.file != None:
            exp = self.phot.run(options.file, self.stars)

    @action(short="l", type="string", helpGroup="Photometry", help="Run photometry on a list of files")
    def list(self, options):
        if options.stars != None:
            self.getStars()

        if options.list != None:
            file = open(options.list, 'r')
            for line in file:
                if line.endswith('\n'): line = line[0:-1]
                exp = self.phot.run(line, self.stars)
            file.close()

    def getStars(self, filename):
        file = open(filename, 'r')
        self.stars = []
        for line in file:
            if line.endswith('\n'): line = line[0:-1]
            star = line.split(',')
            self.stars.append(star)
        file.close()


def main():
    cli = PhotometryCLI()
    cli.run(sys.argv)
    cli.wait()

if __name__ == '__main__':
    main()
